package com.dmxh.service.impl;

import com.dmxh.mapper.TbContentMapper;
import com.dmxh.params.QueryParams;
import com.dmxh.pojo.TbContent;
import com.dmxh.pojo.TbContentExample;
import com.dmxh.result.PageList;
import com.dmxh.service.ContentService;
import com.dmxh.vo.ContentVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper tbContentMapper;

    @Override
    public Map<String, Object> queryAllContent() {
        //查询全部轮播图
        List<TbContent> list1 = tbContentMapper.queryAllContent(1L, 3);
        //查询全部今日推荐
        List<TbContent> list2 = tbContentMapper.queryAllContent(2L, 4);
        //查询全部猜你喜欢
        List<TbContent> list3 = tbContentMapper.queryAllContent(4L, 6);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list1", list1);
        map.put("list2", list2);
        map.put("list3", list3);
        return map;
    }

    @Override
    public PageList<ContentVo> queryPage(QueryParams queryParams) {
        Page<ContentVo> page = PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        List<TbContent> list = tbContentMapper.selectByExample(null);
        PageInfo<ContentVo> info = page.toPageInfo();
        List<ContentVo> collect = list.stream().map(tbContent -> {
            ContentVo contentVo = new ContentVo();
            BeanUtils.copyProperties(tbContent, contentVo);
            return contentVo;
        }).collect(Collectors.toList());
        return new PageList<>(info.getPageNum(), info.getPageSize(), info.getTotal(), collect);
    }

    @Override
    public void addInfo(ContentVo contentVo) {
        TbContent tbContent = new TbContent();
        BeanUtils.copyProperties(contentVo,tbContent);
        tbContentMapper.insertSelective(tbContent);
    }

    @Override
    public void updateInfo(ContentVo contentVo) {
        TbContent tbContent = new TbContent();
        BeanUtils.copyProperties(contentVo,tbContent);
        tbContentMapper.updateByPrimaryKeySelective(tbContent);
    }

    @Override
    public void deleteById(long id) {
        tbContentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        List<Long> collect = list.stream().map(Integer::longValue).collect(Collectors.toList());
        TbContentExample tbContentExample = new TbContentExample();
        tbContentExample.createCriteria().andIdIn(collect);
        tbContentMapper.deleteByExample(tbContentExample);
    }

}
